package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQuerySchemeOpenRecordReq;
import com.sg.dto.base.component.req.QuerySchemeOpenRecordDetailReq;
import com.sg.dto.base.component.req.QuerySchemeOpenRecordListReq;
import com.sg.entity.OmsSchemeOpenRecord;
import com.sg.service.base.component.MOmsSchemeOpenRecordService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsSchemeOpenRecordMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsSchemeOpenRecordServiceImpl
        extends ServiceImpl<OmsSchemeOpenRecordMapper, OmsSchemeOpenRecord>
        implements MOmsSchemeOpenRecordService {

    @Autowired
    OmsSchemeOpenRecordMapper omsSchemeOpenRecordMapper;

    /**
     * 2-3-12查询方案开通记录详情[1954]
     * gen by moon at 4/22/2023, 9:05:33 PM
     */
    @Trace(operationName = "2-3-12查询方案开通记录详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsSchemeOpenRecord querySchemeOpenRecordDetail(QuerySchemeOpenRecordDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsSchemeOpenRecord> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsSchemeOpenRecord> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsSchemeOpenRecord retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-12修改方案开通记录[1952]
     * gen by moon at 10/16/2022, 11:04:27 AM
     */
    @Trace(operationName = "2-3-12修改方案开通记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSchemeOpenRecord(OmsSchemeOpenRecord omsSchemeOpenRecord) {
        omsSchemeOpenRecord.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsSchemeOpenRecord != null && omsSchemeOpenRecord.getSubjectLifeCycle() != null && omsSchemeOpenRecord.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsSchemeOpenRecord.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsSchemeOpenRecord != null && omsSchemeOpenRecord.getSubjectLifeCycle() != null && omsSchemeOpenRecord.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsSchemeOpenRecord.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsSchemeOpenRecord.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsSchemeOpenRecord != null && omsSchemeOpenRecord.getSubjectLifeCycle() != null && omsSchemeOpenRecord.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsSchemeOpenRecord.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsSchemeOpenRecord != null && omsSchemeOpenRecord.getSubjectLifeCycle() != null && omsSchemeOpenRecord.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsSchemeOpenRecord.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsSchemeOpenRecord);
    }

    /**
     * 2-3-12新增方案开通记录[1951]
     * gen by moon at 10/16/2022, 11:04:26 AM
     */
    @Trace(operationName = "2-3-12新增方案开通记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addSchemeOpenRecord(OmsSchemeOpenRecord omsSchemeOpenRecord) {
        omsSchemeOpenRecord.setSchOpenRecordId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsSchemeOpenRecord.setSchOpenRecordCode(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsSchemeOpenRecord.setIsArchive("FALSE");
        omsSchemeOpenRecord.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsSchemeOpenRecord.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsSchemeOpenRecord != null && omsSchemeOpenRecord.getSubjectLifeCycle() != null && omsSchemeOpenRecord.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsSchemeOpenRecord.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsSchemeOpenRecord.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsSchemeOpenRecord.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsSchemeOpenRecord != null && omsSchemeOpenRecord.getSubjectLifeCycle() != null && omsSchemeOpenRecord.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsSchemeOpenRecord.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsSchemeOpenRecord.setIsValid("FALSE");
        omsSchemeOpenRecord.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsSchemeOpenRecord.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsSchemeOpenRecord)) {
            return omsSchemeOpenRecord.getSchOpenRecordId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-3-12查询方案开通记录列表[1955]
     * gen by moon at 10/16/2022, 11:04:29 AM
     */
    @Trace(operationName = "2-3-12查询方案开通记录列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsSchemeOpenRecord> querySchemeOpenRecordList(QuerySchemeOpenRecordListReq reqDto) {

        try {
            QueryWrapper<OmsSchemeOpenRecord> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsSchemeOpenRecord.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsSchemeOpenRecord.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsSchemeOpenRecord.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsSchemeOpenRecord>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-12删除方案开通记录[1953]
     * gen by moon at 10/16/2022, 11:04:27 AM
     */
    @Trace(operationName = "2-3-12删除方案开通记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSchemeOpenRecord(String schOpenRecordId) {

        return super.removeById(schOpenRecordId);
    }

    /**
     * 2-3-12批量查询方案开通记录[4317]
     * gen by moon at 10/16/2022, 11:04:29 AM
     */
    @Trace(operationName = "2-3-12批量查询方案开通记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsSchemeOpenRecord> batchQuerySchemeOpenRecord(BatchQuerySchemeOpenRecordReq reqDto) {

        try {
            QueryWrapper<OmsSchemeOpenRecord> queryWrapper = new QueryWrapper<>();

            if (reqDto.getSchemeOpenRecordList() == null || reqDto.getSchemeOpenRecordList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_scheme_id", reqDto.getSchemeOpenRecordList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsSchemeOpenRecord.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsSchemeOpenRecord.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsSchemeOpenRecord.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsSchemeOpenRecord>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M2查询方案开通记录测试列表[4889]
     * gen by moon at 10/16/2022, 11:04:30 AM
     */
    @Trace(operationName = "M2查询方案开通记录测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsSchemeOpenRecord> querySchemeRecordDataList(OmsSchemeOpenRecord omsSchemeOpenRecord) {

        try {
            QueryWrapper<OmsSchemeOpenRecord> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsSchemeOpenRecord);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsSchemeOpenRecord.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsSchemeOpenRecord.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsSchemeOpenRecord.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsSchemeOpenRecord>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M2批量删方案开通记录测试[4890]
     * gen by moon at 10/16/2022, 11:04:31 AM
     */
    @Trace(operationName = "M2批量删方案开通记录测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteSchemeRecordData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-3-12批量删方案开通记录[6464]
     * gen by moon at 5/5/2023, 9:47:56 PM
     */
    @Trace(operationName = "2-3-12批量删方案开通记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteSchemeOpenRecord(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-3-12修改开通记录[6471]
     * gen by moon at 5/5/2023, 9:47:57 PM
     */
    @Trace(operationName = "2-3-12修改开通记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSchOpenRecord(OmsSchemeOpenRecord omsSchemeOpenRecord) {

        return super.updateById(omsSchemeOpenRecord);
    }
}
